package com.nari.ysphd.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.orhanobut.logger.Logger;

import java.util.Locale;

/**
 * 类描述:
 * 创建人:   tangchao
 * 创建时间: 18/12/28 09:00
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao
 * 修改时间: 18/12/28 09:00
 * 修改备注:  [说明本次修改内容]
 */
public class EmulatorUtil {

    //    链接：https://www.jianshu.com/p/e71c7d02ba44
    public static boolean isEmulator(Context mContext) {
        String url = "tel:" + "123456";
        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_DIAL);
        // 是否可以处理跳转到拨号的 Intent
        boolean canResolveIntent = intent.resolveActivity(mContext.getPackageManager()) != null;

        Logger.d("EmulatorUtil===isEmulator:     + Build.FINGERPRINT.startsWith(\"generic\") );           " + Build.FINGERPRINT.startsWith("generic"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.FINGERPRINT.toLowerCase().contains(\"vbox\") );        " + Build.FINGERPRINT.toLowerCase(Locale.ENGLISH).contains("vbox"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.FINGERPRINT.toLowerCase().contains(\"test-keys\") );       " + Build.FINGERPRINT.toLowerCase(Locale.ENGLISH).contains("test-keys"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.MODEL.contains(\"google_sdk\") );      " + Build.MODEL.contains("google_sdk"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.MODEL.contains(\"Emulator\") );     " + Build.MODEL.contains("Emulator"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.SERIAL.equalsIgnoreCase(\"unknown\") );       " + Build.SERIAL.equalsIgnoreCase("unknown"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.SERIAL.equalsIgnoreCase(\"android\") );        " + Build.SERIAL.equalsIgnoreCase("android"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.MODEL.contains(\"Android SDK built for x86\") );      " + Build.MODEL.contains("Android SDK built for x86"));
        Logger.d("EmulatorUtil===isEmulator:     + Build.MANUFACTURER.contains(\"Genymotion\") );        " + Build.MANUFACTURER.contains("Genymotion"));
        Logger.d("EmulatorUtil===isEmulator:     + (Build.BRAND.startsWith(\"generic\") && Build.DEVICE.startsWith(\"generic\")) );        " + (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")));
        Logger.d("EmulatorUtil===isEmulator:     + \"google_sdk\".equals(Build.PRODUCT) );       " + "google_sdk".equals(Build.PRODUCT));

        Logger.d("EmulatorUtil===isEmulator:     +tNetworkOperatorName().toLowerCase().equals(\"android\") );            " + ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE))
                .getNetworkOperatorName().toLowerCase(Locale.ENGLISH).equals("android"));

        Logger.d("EmulatorUtil===isEmulator: !canResolveIntent=" + !canResolveIntent);

        return Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.toLowerCase(Locale.ENGLISH).contains("vbox")
                || Build.FINGERPRINT.toLowerCase(Locale.ENGLISH).contains("test-keys")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
//                || Build.SERIAL.equalsIgnoreCase("unknown") //Android 9 及其之后为了保护用户隐私返回都是unknown
                || Build.SERIAL.equalsIgnoreCase("android")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk".equals(Build.PRODUCT)
                || ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE))
                .getNetworkOperatorName().toLowerCase(Locale.ENGLISH).equals("android")
                || !canResolveIntent;
    }

}
